SUBROUTINE WRTRES
USE PARKIND1  ,ONLY : JPIM     ,JPRB,JPRM,JPRD
USE YOMHOOK   ,ONLY : LHOOK    ,DR_HOOK, JPHOOK
! (C) Copyright 2000- ECMWF.
!
! This software is licensed under the terms of the Apache Licence Version 2.0
! which can be obtained at http://www.apache.org/licenses/LICENSE-2.0.
! In applying this licence, ECMWF does not waive the privileges and immunities
! granted to it by virtue of its status as an intergovernmental organisation
! nor does it submit to any jurisdiction.

!**** *WRTRES*  - writes NetCDF restart file

!     Purpose.
!     --------
!        writes out restart file

!**   Interface.
!     ----------
!        *CALL* *WRTRES

!     Explicit arguments :
!     --------------------

!     Implicit arguments :
!     --------------------

!     Method.
!     -------
!        See documentation

!     Externals.
!     ----------
!        None

!     Reference.
!     ----------

!     Author.
!     -------
!        Bart vd Hurk (KNMI)

!     Modifications.
!     --------------
!        original: 17/7/2000
!        E. DUTRA     02/2009 - ADD LAKES RELATED
!        E. Dutra     11/2009 - Add LAI restart

USE YOMLUN1S , ONLY : NPOSRES  ,NULOUT ,RMISS
USE YOMLOG1S , ONLY : NDIMCDF,LNCSNC
USE YOETHF   , ONLY : RHOH2O
USE YOMDYN1S , ONLY : NSTEP    ,TSTEP
USE YOMCC1S  , ONLY : VCALB,VCLAIL,VCLAIH,VCFWET,VCAVGPAR
USE YOMGP1S0 , ONLY : TSLNU0   ,QLINU0   ,TILNU0   ,FSNNU0   ,&
            &TSNNU0   ,ASNNU0   ,RSNNU0   ,WRENU0   ,TRENU0,&
            &TLICENU0,TLMNWNU0,TLWMLNU0,TLBOTNU0,TLSFNU0,& !FLAKE
            &HLICENU0,HLMLNU0, &                           !FLAKE 
            &LAINU0,BSTRNU0, BSTR2NU0,WSNNU0

USE YOMGPD1S , ONLY : VFZ0F    ,VFZ0H     ,VFITM    ,VFCVL &
                     &,VFCVH   ,VFCUR     ,VFTVL  ,VFCO2TYP  ,VFISOP_EP, VFTVH    ,VFSST &
                     &,VFCI     ,VFCIL, VFSOTY    ,VFSDOR  ,VFGEO &
                     &,VDIEWSSTL ,VDINSSSTL,VDISSHFTL &
                     &,VDIETL   ,VDTSKTL,VFLDEPTH,VFCLAKE,VFCLAKEF &
                     &,VFR0VT   ,VDANDAYVT,VDANFMVT &
                     &,VDRESPBSTR,VDRESPBSTR2,VDBIOMASS_LAST&
            	     &,VFPGLOB

USE YOMDPHY  , ONLY : NLAT     ,NLON      ,NPOI     ,NPOIOFF     ,NTILES &
                     &,NLALO     ,NCSS &
                     &,NVHILO, NCLIMDAT,NCSNEC,NPOIP

USE YOESOIL1S, ONLY : RDAW
USE YOMRIP   , ONLY : RSTATI
USE YOMGC1S  , ONLY : LMASK
USE YOEPHY   , ONLY : LEVGEN   ,LESSRO, LEFLAKE, LECTESSEL,LESNML, LEC4MAP
USE MPL_MODULE


IMPLICIT NONE

!* Netcdf Interface
INTEGER NPOS,IERR,NSTPP,NVARID
REAL(KIND=JPRD) ZTIME
INTEGER ISTART1(1),ISTART2(2),ISTART3(3),ISTART4(4)
INTEGER ICOUNT1(1),ICOUNT2(2),ICOUNT3(3),ICOUNT4(4)
REAL(KIND=JPRD),ALLOCATABLE :: ZMASK(:)
REAL(KIND=JPRD),ALLOCATABLE :: ZOUTPUT(:,:)

!* Local declarations
REAL(KIND=JPRB),ALLOCATABLE :: ZQLINU0(:,:)
INTEGER(KIND=JPIM) :: IX1,IX2,IY1,IY2,NLEN,IST,IEN,ILEVS,JM,JTILE,JK,JVT,ISTP,IENP
REAL(KIND=JPRB),ALLOCATABLE ::ZBUF(:)
REAL(KIND=JPRB),ALLOCATABLE ::ZVALUE(:)
INTEGER(KIND=JPIM) :: MYPROC, NPROC
REAL(KIND=JPHOOK)    :: ZHOOK_HANDLE
INTEGER(KIND=JPIM) :: NMXNV

#include "netcdf.inc"
#include "minmax.intfb.h"

IF (LHOOK) CALL DR_HOOK('WRTRES',0,ZHOOK_HANDLE)

MYPROC = MPL_MYRANK()
NPROC  = MPL_NPROC()

ISTP   = NPOIOFF(1)+1
IENP   = NPOIOFF(NPROC+1)

WRITE(NULOUT,*) 'WRTDRES'


!     ------------------------------------------------------------------

!*       1.   SCALING SOME VARIABLES.
!             -----------------------

!     NOTE: UNITS OF FSNNU,WRENU IN THE SURF1D MODEL:            KG/M2
!           UNITS OF SNOW AND INTERCEP RESER WATER IN THE
!                                     PHYSICS:                   KG/M2
!           UNITS OF SNOW AND INTERCEP RESER WATER IN THE
!                                     INPUT AND OUTPUT FILES:    KG/M2=MM

IF(NDIMCDF == 2)THEN
  IX1=1
  IX2=NLON
  IY1=1
  IY2=NLAT
  NLEN=(IX2-IX1+1)*(IY2-IY1+1)
  IST=IX1+(IY1-1)*NLON
  IEN=IST+NLEN-1
ELSE
  IST=1
  IEN=NLALO
  NLEN=IEN-IST+1
ENDIF

! max size for second dimension of output so that we can reuse ZOUTPUT
NMXNV=MAX(NCLIMDAT,MAX(MAX(MAX(NTILES,NCSS),NCSNEC),NVHILO))

ALLOCATE (ZOUTPUT(NLEN,NMXNV))            !INFLATED OUTPUT ARRAY
!ALLOCATE (ZOUTPUTA(NLEN,NCLIMDAT))  !INFLATED OUTPUT ARRAY
!ALLOCATE (ZOUTPUTT(NLEN,NTILES))
!ALLOCATE (ZOUTPUTS(NLEN,NCSS))
!ALLOCATE (ZOUTPUTSN(NLEN,NCSNEC))
!ALLOCATE (ZOUTPUTV(NLEN,NVHILO))


ALLOCATE (ZVALUE(NPOI))
ALLOCATE (ZQLINU0(NPOI,NCSS))
DO ILEVS=1,NCSS
  ZQLINU0(:,ILEVS)=QLINU0(:,ILEVS)*RDAW(ILEVS)*RHOH2O
ENDDO

ALLOCATE (ZBUF(NLEN))

NPOS = NPOSRES

!* -- write spatial and time coordinates

!     time
ISTART1(1)=1
ZTIME=RSTATI-TSTEP*0.5_JPRB
IF( MYPROC == 1 ) THEN
  NVARID = NCVID(NPOS,'time',IERR)
  CALL NCVPT1 (NPOS,NVARID,ISTART1,ZTIME,IERR)
  NVARID = NCVID(NPOS,'timestp',IERR)
  NSTPP=NSTEP
  CALL NCVPT1 (NPOS,NVARID,ISTART1,NSTPP,IERR)
ENDIF
!* -- Surface characteristics
!***********************************************************************
IF(NDIMCDF == 2)THEN
  ISTART2(1)=IX1
  ISTART2(2)=IY1
  ICOUNT2(1)=IX2-IX1+1
  ICOUNT2(2)=IY2-IY1+1
ELSE
  ISTART2(1)=IST
  ICOUNT2(1)=NLEN
  ICOUNT2(2)=1
ENDIF

!     catchment mask
ALLOCATE (ZMASK(NLEN))
ZMASK(:)=RMISS
WHERE (LMASK(ISTP:IENP))
  ZMASK=1._JPRB
END WHERE

IF( MYPROC == 1 ) THEN
  NVARID = NCVID(NPOS,'Mask',IERR)
  CALL NCVPT (NPOS,NVARID,ISTART2,ICOUNT2,ZMASK,IERR)
ENDIF

DEALLOCATE(ZMASK)

!     momentum roughness
CALL MPL_GATHERV(PRECVBUF=ZBUF(:),KROOT=1,PSENDBUF=VFZ0F(:),KRECVCOUNTS=NPOIP(:),CDSTRING="WRTRES:z0m")
IF( MYPROC == 1 ) THEN
  NVARID = NCVID(NPOS,'z0m',IERR)
  ZOUTPUT(:,1)=UNPACK(ZBUF(:),LMASK(ISTP:IENP),RMISS)
  CALL NCVPT (NPOS,NVARID,ISTART2,ICOUNT2,ZOUTPUT(:,1),IERR)
ENDIF

!     heat roughness
ZVALUE(:)=LOG(VFZ0H(:))
CALL MPL_GATHERV(PRECVBUF=ZBUF(:),KROOT=1,PSENDBUF=ZVALUE(:),KRECVCOUNTS=NPOIP(:),CDSTRING="WRTRES:lz0h")
IF( MYPROC == 1 ) THEN
  NVARID = NCVID(NPOS,'lz0h',IERR)
  ZOUTPUT(:,1)=UNPACK(ZBUF(:),LMASK(ISTP:IENP),RMISS)
  CALL NCVPT (NPOS,NVARID,ISTART2,ICOUNT2,ZOUTPUT(:,1),IERR)
ENDIF
DEALLOCATE (ZVALUE)

!     monthly albedo
IF(NDIMCDF == 2)THEN
  ISTART3(1)=IX1
  ISTART3(2)=IY1
  ISTART3(3)=1
  ICOUNT3(1)=IX2-IX1+1
  ICOUNT3(2)=IY2-IY1+1
  ICOUNT3(3)=NCLIMDAT
ELSE
  ISTART3(1)=IST
  ISTART3(2)=1
  ICOUNT3(1)=NLEN
  ICOUNT3(2)=NCLIMDAT
ENDIF
DO JM=1,NCLIMDAT
  CALL MPL_GATHERV(PRECVBUF=ZBUF(:),KROOT=1,PSENDBUF=VCALB(:,JM),KRECVCOUNTS=NPOIP(:),CDSTRING="WRTRES:Malbedo")
  IF( MYPROC == 1 ) THEN
    ZOUTPUT(:,JM)=UNPACK(ZBUF(:),LMASK(ISTP:IENP),RMISS)
  ENDIF
ENDDO

IF( MYPROC == 1 ) THEN
  NVARID = NCVID(NPOS,'Malbedo',IERR)
  CALL NCVPT (NPOS,NVARID,ISTART3,ICOUNT3,ZOUTPUT,IERR)
ENDIF

! LAI low veg
DO JM=1,NCLIMDAT
  CALL MPL_GATHERV(PRECVBUF=ZBUF(:),KROOT=1,PSENDBUF=VCLAIL(:,JM),KRECVCOUNTS=NPOIP(:),CDSTRING="WRTRES:Mlail")
  IF( MYPROC == 1 ) THEN
    ZOUTPUT(:,JM)=UNPACK(ZBUF(:),LMASK(ISTP:IENP),RMISS)
  ENDIF
ENDDO

IF( MYPROC == 1 ) THEN
  NVARID = NCVID(NPOS,'Mlail',IERR)
  CALL NCVPT (NPOS,NVARID,ISTART3,ICOUNT3,ZOUTPUT,IERR)
ENDIF

! LAI high veg
DO JM=1,NCLIMDAT
  CALL MPL_GATHERV(PRECVBUF=ZBUF(:),KROOT=1,PSENDBUF=VCLAIH(:,JM),KRECVCOUNTS=NPOIP(:),CDSTRING="WRTRES:Mlaih")
  IF( MYPROC == 1 ) THEN
    ZOUTPUT(:,JM)=UNPACK(ZBUF(:),LMASK(ISTP:IENP),RMISS)
  ENDIF
ENDDO

IF( MYPROC == 1 ) THEN
  NVARID = NCVID(NPOS,'Mlaih',IERR)
  CALL NCVPT (NPOS,NVARID,ISTART3,ICOUNT3,ZOUTPUT,IERR)
ENDIF


! wetland fraction
DO JM=1,NCLIMDAT
  CALL MPL_GATHERV(PRECVBUF=ZBUF(:),KROOT=1,PSENDBUF=VCFWET(:,JM),KRECVCOUNTS=NPOIP(:),CDSTRING="WRTRES:Fwet")
  IF( MYPROC == 1 ) THEN
    ZOUTPUT(:,JM)=UNPACK(ZBUF(:),LMASK(ISTP:IENP),RMISS)
  ENDIF
ENDDO

IF( MYPROC == 1 ) THEN
  NVARID = NCVID(NPOS,'fwet',IERR)
  CALL NCVPT (NPOS,NVARID,ISTART3,ICOUNT3,ZOUTPUT,IERR)
ENDIF


! average PAR
DO JM=1,NCLIMDAT
  CALL MPL_GATHERV(PRECVBUF=ZBUF(:),KROOT=1,PSENDBUF=VCAVGPAR(:,JM),KRECVCOUNTS=NPOIP(:),CDSTRING="WRTRES:par_avg")
  IF( MYPROC == 1 ) THEN
    ZOUTPUT(:,JM)=UNPACK(ZBUF(:),LMASK(ISTP:IENP),RMISS)
  ENDIF
ENDDO

IF( MYPROC == 1 ) THEN
  NVARID = NCVID(NPOS,'par_avg',IERR)
  CALL NCVPT (NPOS,NVARID,ISTART3,ICOUNT3,ZOUTPUT,IERR)
ENDIF

!     land sea mask
CALL MPL_GATHERV(PRECVBUF=ZBUF(:),KROOT=1,PSENDBUF=VFITM(:),KRECVCOUNTS=NPOIP(:),CDSTRING="WRTRES:landsea")
IF( MYPROC == 1 ) THEN
  NVARID = NCVID(NPOS,'landsea',IERR)
  ZOUTPUT(:,1)=UNPACK(ZBUF(:),LMASK(ISTP:IENP),RMISS)
  CALL NCVPT (NPOS,NVARID,ISTART2,ICOUNT2,ZOUTPUT(:,1),IERR)
ENDIF

!     geopotential
CALL MPL_GATHERV(PRECVBUF=ZBUF(:),KROOT=1,PSENDBUF=VFGEO(:),KRECVCOUNTS=NPOIP(:),CDSTRING="WRTRES:geopot")
IF( MYPROC == 1 ) THEN
  NVARID = NCVID(NPOS,'geopot',IERR)
  ZOUTPUT(:,1)=UNPACK(ZBUF(:),LMASK(ISTP:IENP),RMISS)
  CALL NCVPT (NPOS,NVARID,ISTART2,ICOUNT2,ZOUTPUT(:,1),IERR)
ENDIF

!     low vegetation coverage
CALL MPL_GATHERV(PRECVBUF=ZBUF(:),KROOT=1,PSENDBUF=VFCVL(:),KRECVCOUNTS=NPOIP(:),CDSTRING="WRTRES:cvl")
IF( MYPROC == 1 ) THEN
  NVARID = NCVID(NPOS,'cvl',IERR)
  ZOUTPUT(:,1)=UNPACK(ZBUF(:),LMASK(ISTP:IENP),RMISS)
  CALL NCVPT (NPOS,NVARID,ISTART2,ICOUNT2,ZOUTPUT(:,1),IERR)
ENDIF

!     high vegetation coverage
CALL MPL_GATHERV(PRECVBUF=ZBUF(:),KROOT=1,PSENDBUF=VFCVH(:),KRECVCOUNTS=NPOIP(:),CDSTRING="WRTRES:cvh")
IF( MYPROC == 1 ) THEN
  NVARID = NCVID(NPOS,'cvh',IERR)
  ZOUTPUT(:,1)=UNPACK(ZBUF(:),LMASK(ISTP:IENP),RMISS)
  CALL NCVPT (NPOS,NVARID,ISTART2,ICOUNT2,ZOUTPUT(:,1),IERR)
ENDIF

!     low vegetation type
CALL MPL_GATHERV(PRECVBUF=ZBUF(:),KROOT=1,PSENDBUF=VFTVL(:),KRECVCOUNTS=NPOIP(:),CDSTRING="WRTRES:tvl")
IF( MYPROC == 1 ) THEN
  NVARID = NCVID(NPOS,'tvl',IERR)
  ZOUTPUT(:,1)=UNPACK(ZBUF(:),LMASK(ISTP:IENP),RMISS)
  CALL NCVPT (NPOS,NVARID,ISTART2,ICOUNT2,ZOUTPUT(:,1),IERR)
ENDIF

!     high vegetation type
CALL MPL_GATHERV(PRECVBUF=ZBUF(:),KROOT=1,PSENDBUF=VFTVH(:),KRECVCOUNTS=NPOIP(:),CDSTRING="WRTRES:tvh")
IF( MYPROC == 1 ) THEN
  NVARID = NCVID(NPOS,'tvh',IERR)
  ZOUTPUT(:,1)=UNPACK(ZBUF(:),LMASK(ISTP:IENP),RMISS)
  CALL NCVPT (NPOS,NVARID,ISTART2,ICOUNT2,ZOUTPUT(:,1),IERR)
ENDIF

!     urban coverage
CALL MPL_GATHERV(PRECVBUF=ZBUF(:),KROOT=1,PSENDBUF=VFCUR(:),KRECVCOUNTS=NPOIP(:),CDSTRING="WRTRES:cu")
IF( MYPROC == 1 ) THEN
  NVARID = NCVID(NPOS,'cu',IERR)
  ZOUTPUT(:,1)=UNPACK(ZBUF(:),LMASK(ISTP:IENP),RMISS)
  CALL NCVPT (NPOS,NVARID,ISTART2,ICOUNT2,ZOUTPUT(:,1),IERR)
ENDIF

IF (LEC4MAP) THEN
!     CO2 photosynthetic pathway (C3/C4 vegetation type)
  CALL MPL_GATHERV(PRECVBUF=ZBUF(:),KROOT=1,PSENDBUF=VFCO2TYP(:),KRECVCOUNTS=NPOIP(:),CDSTRING="WRTRES:Ctype")
  IF( MYPROC == 1 ) THEN
    NVARID = NCVID(NPOS,'Ctype',IERR)
!  ZOUTPUT(:,1)=UNPACK(VFCO2TYP,LMASK(IST:IEN),RMISS)
    ZOUTPUT(:,1)=UNPACK(ZBUF(:),LMASK(IST:IEN),RMISS)
    CALL NCVPT (NPOS,NVARID,ISTART2,ICOUNT2,ZOUTPUT,IERR)
  ENDIF
ENDIF

! Isoprene emission potential
CALL MPL_GATHERV(PRECVBUF=ZBUF(:),KROOT=1,PSENDBUF=VFISOP_EP(:),KRECVCOUNTS=NPOIP(:),CDSTRING="WRTRES:ISOP_EP")
IF( MYPROC == 1 ) THEN
  NVARID = NCVID(NPOS,'ISOP_EP',IERR)
  ZOUTPUT(:,1)=UNPACK(ZBUF(:),LMASK(IST:IEN),RMISS)
  CALL NCVPT (NPOS,NVARID,ISTART2,ICOUNT2,ZOUTPUT,IERR)
ENDIF

!     soil type
CALL MPL_GATHERV(PRECVBUF=ZBUF(:),KROOT=1,PSENDBUF=VFSOTY(:),KRECVCOUNTS=NPOIP(:),CDSTRING="WRTRES:sotype")
IF( MYPROC == 1 ) THEN
  NVARID = NCVID(NPOS,'sotype',IERR)
  ZOUTPUT(:,1)=UNPACK(ZBUF(:),LMASK(ISTP:IENP),RMISS)
  CALL NCVPT (NPOS,NVARID,ISTART2,ICOUNT2,ZOUTPUT(:,1),IERR)
ENDIF

!     orographic variance
CALL MPL_GATHERV(PRECVBUF=ZBUF(:),KROOT=1,PSENDBUF=VFSDOR(:),KRECVCOUNTS=NPOIP(:),CDSTRING="WRTRES:sdor")
IF( MYPROC == 1 ) THEN
  NVARID = NCVID(NPOS,'sdor',IERR)
  ZOUTPUT(:,1)=UNPACK(ZBUF(:),LMASK(ISTP:IENP),RMISS)
  CALL NCVPT (NPOS,NVARID,ISTART2,ICOUNT2,ZOUTPUT(:,1),IERR)
ENDIF

!     sst
CALL MPL_GATHERV(PRECVBUF=ZBUF(:),KROOT=1,PSENDBUF=VFSST(:),KRECVCOUNTS=NPOIP(:),CDSTRING="WRTRES:sst")
IF( MYPROC == 1 ) THEN
  NVARID = NCVID(NPOS,'sst',IERR)
  ZOUTPUT(:,1)=UNPACK(ZBUF(:),LMASK(ISTP:IENP),RMISS)
  CALL NCVPT (NPOS,NVARID,ISTART2,ICOUNT2,ZOUTPUT(:,1),IERR)
ENDIF

!     sea ice fraction
CALL MPL_GATHERV(PRECVBUF=ZBUF(:),KROOT=1,PSENDBUF=VFCI(:),KRECVCOUNTS=NPOIP(:),CDSTRING="WRTRES:seaice")
IF( MYPROC == 1 ) THEN
  NVARID = NCVID(NPOS,'seaice',IERR)
  ZOUTPUT(:,1)=UNPACK(ZBUF,LMASK(ISTP:IENP),RMISS)
  CALL NCVPT (NPOS,NVARID,ISTART2,ICOUNT2,ZOUTPUT(:,1),IERR)
ENDIF

!     land ice fraction
CALL MPL_GATHERV(PRECVBUF=ZBUF(:),KROOT=1,PSENDBUF=VFCIL(:),KRECVCOUNTS=NPOIP(:),CDSTRING="WRTRES:glacierMask")
IF( MYPROC == 1 ) THEN
  NVARID = NCVID(NPOS,'glacierMask',IERR)
  ZOUTPUT(:,1)=UNPACK(ZBUF,LMASK(ISTP:IENP),RMISS)
  CALL NCVPT (NPOS,NVARID,ISTART2,ICOUNT2,ZOUTPUT(:,1),IERR)
ENDIF


!     x gaussian reduced index
CALL MPL_GATHERV(PRECVBUF=ZBUF(:),KROOT=1,PSENDBUF=VFPGLOB(:),KRECVCOUNTS=NPOIP(:),CDSTRING="WRTRES:gaussianIndex")
IF ( MYPROC == 1 ) THEN
  NVARID = NCVID(NPOS,'x',IERR)
  ZOUTPUT(:,1)=UNPACK(ZBUF,LMASK(ISTP:IENP),RMISS)
  CALL NCVPT (NPOS,NVARID,ISTART2,ICOUNT2,ZOUTPUT,IERR)
ENDIF


!* -- Tile specific quantities
!***********************************************************************
!     sensible heat flux
IF(NDIMCDF == 2)THEN
  ISTART3(1)=IX1
  ISTART3(2)=IY1
  ISTART3(3)=1
  ICOUNT3(1)=IX2-IX1+1
  ICOUNT3(2)=IY2-IY1+1
  ICOUNT3(3)=NTILES
ELSE
  ISTART3(1)=IST
  ISTART3(2)=1
  ICOUNT3(1)=NLEN
  ICOUNT3(2)=NTILES
ENDIF

DO JTILE=1,NTILES
  CALL MPL_GATHERV(PRECVBUF=ZBUF(:),KROOT=1,PSENDBUF=VDISSHFTL(:,JTILE),KRECVCOUNTS=NPOIP(:),CDSTRING="WRTRES:Qh")
  IF( MYPROC == 1 ) THEN
    ZOUTPUT(:,JTILE)=UNPACK(ZBUF(:),LMASK(ISTP:IENP),RMISS)
    ! Write out log of restart file to check bit-repro
    WRITE(NULOUT,'(A6,I2.2)')'Qh,JT:',JTILE
    CALL MINMAX('Qh',REAL(ZOUTPUT(:,JTILE),KIND=JPRB),ICOUNT2(1),ICOUNT2(2),LMASK(ISTP:IENP),NULOUT)
  ENDIF
ENDDO

IF( MYPROC == 1 ) THEN
  NVARID = NCVID(NPOS,'Qh',IERR)
  CALL NCVPT (NPOS,NVARID,ISTART3,ICOUNT3,ZOUTPUT,IERR)
ENDIF

!     latent heat flux
DO JTILE=1,NTILES
  CALL MPL_GATHERV(PRECVBUF=ZBUF(:),KROOT=1,PSENDBUF=VDIETL(:,JTILE),KRECVCOUNTS=NPOIP(:),CDSTRING="WRTRES:Evap")
  IF( MYPROC == 1 ) THEN
    ZOUTPUT(:,JTILE)=UNPACK(ZBUF(:),LMASK(ISTP:IENP),RMISS)
    ! Write out log of restart file to check bit-repro
    WRITE(NULOUT,'(A8,I2.2)')'Evap,JT:',JTILE 
    CALL MINMAX('Evap',REAL(ZOUTPUT(:,JTILE),KIND=JPRB),ICOUNT2(1),ICOUNT2(2),LMASK(ISTP:IENP),NULOUT)
  ENDIF
ENDDO

IF( MYPROC == 1 ) THEN
  NVARID = NCVID(NPOS,'Evap',IERR)
  CALL NCVPT (NPOS,NVARID,ISTART3,ICOUNT3,ZOUTPUT,IERR)
ENDIF

!     momentum flux u

DO JTILE=1,NTILES
  CALL MPL_GATHERV(PRECVBUF=ZBUF(:),KROOT=1,PSENDBUF=VDIEWSSTL(:,JTILE),KRECVCOUNTS=NPOIP(:),CDSTRING="WRTRES:Momu")
  IF( MYPROC == 1 ) THEN
    ZOUTPUT(:,JTILE)=UNPACK(ZBUF(:),LMASK(ISTP:IENP),RMISS)
    ! Write out log of restart file to check bit-repro
    WRITE(NULOUT,'(A8,I2.2)')'Momu,JT:',JTILE 
    CALL MINMAX('Momu',REAL(ZOUTPUT(:,JTILE),KIND=JPRB),ICOUNT2(1),ICOUNT2(2),LMASK(ISTP:IENP),NULOUT)
  ENDIF
ENDDO

IF( MYPROC == 1 ) THEN
  NVARID = NCVID(NPOS,'Momu',IERR)
  CALL NCVPT (NPOS,NVARID,ISTART3,ICOUNT3,ZOUTPUT,IERR)
ENDIF

!     momentum flux v
DO JTILE=1,NTILES
  CALL MPL_GATHERV(PRECVBUF=ZBUF(:),KROOT=1,PSENDBUF=VDINSSSTL(:,JTILE),KRECVCOUNTS=NPOIP(:),CDSTRING="WRTRES:Momv")
  IF( MYPROC == 1 ) THEN
    ZOUTPUT(:,JTILE)=UNPACK(ZBUF(:),LMASK(ISTP:IENP),RMISS)
    ! Write out log of restart file to check bit-repro
    WRITE(NULOUT,'(A8,I2.2)')'Momv,JT:',JTILE 
    CALL MINMAX('Momv',REAL(ZOUTPUT(:,JTILE),KIND=JPRB),ICOUNT2(1),ICOUNT2(2),LMASK(ISTP:IENP),NULOUT)
  ENDIF
ENDDO

IF( MYPROC == 1 ) THEN
  NVARID = NCVID(NPOS,'Momv',IERR)
  CALL NCVPT (NPOS,NVARID,ISTART3,ICOUNT3,ZOUTPUT,IERR)
ENDIF

!     skin temperature
IF( MYPROC == 1 ) NVARID = NCVID(NPOS,'SkinT',IERR)
DO JTILE=1,NTILES
  CALL MPL_GATHERV(PRECVBUF=ZBUF(:),KROOT=1,PSENDBUF=VDTSKTL(:,JTILE),KRECVCOUNTS=NPOIP(:),CDSTRING="WRTRES:SkinT")
  IF( MYPROC == 1 ) THEN
    ZOUTPUT(:,JTILE)=UNPACK(ZBUF(:),LMASK(ISTP:IENP),RMISS)
    ! Write out log of restart file to check bit-repro
    WRITE(NULOUT,'(A9,I2.2)')'SkinT,JT:',JTILE 
    CALL MINMAX('SkinT',REAL(ZOUTPUT(:,JTILE),KIND=JPRB),ICOUNT2(1),ICOUNT2(2),LMASK(ISTP:IENP),NULOUT)
  ENDIF
ENDDO

IF( MYPROC == 1 ) THEN
  NVARID = NCVID(NPOS,'SkinT',IERR)
  CALL NCVPT (NPOS,NVARID,ISTART3,ICOUNT3,ZOUTPUT,IERR)
ENDIF

!*-- Prognostic quantities
!***********************************************************************
!     skin layer content
CALL MPL_GATHERV(PRECVBUF=ZBUF(:),KROOT=1,PSENDBUF=WRENU0(:),KRECVCOUNTS=NPOIP(:),CDSTRING="WRTRES:CanopInt")
IF( MYPROC == 1 ) THEN
  ZOUTPUT(:,1)=UNPACK(ZBUF(:),LMASK(ISTP:IENP),RMISS)
  NVARID = NCVID(NPOS,'CanopInt',IERR)
  CALL NCVPT (NPOS,NVARID,ISTART2,ICOUNT2,ZOUTPUT(:,1),IERR)
  ! Write out log of restart file to check bit-repro
  CALL MINMAX('CanopInt',REAL(ZOUTPUT(:,1),KIND=JPRB),ICOUNT2(1),ICOUNT2(2),LMASK(ISTP:IENP),NULOUT)
ENDIF

!     snow mass
CALL MPL_GATHERV(PRECVBUF=ZBUF(:),KROOT=1,PSENDBUF=FSNNU0(:,1),KRECVCOUNTS=NPOIP(:),CDSTRING="WRTRES:SWE")
IF( MYPROC == 1 ) THEN
  NVARID = NCVID(NPOS,'SWE',IERR)
  ZOUTPUT(:,1)=UNPACK(ZBUF(:),LMASK(ISTP:IENP),RMISS)
  CALL NCVPT (NPOS,NVARID,ISTART2,ICOUNT2,ZOUTPUT(:,1),IERR)
ENDIF

!     snow temp
CALL MPL_GATHERV(PRECVBUF=ZBUF(:),KROOT=1,PSENDBUF=TSNNU0(:,1),KRECVCOUNTS=NPOIP(:),CDSTRING="WRTRES:SnowT")
IF( MYPROC == 1 ) THEN
  NVARID = NCVID(NPOS,'SnowT',IERR)
  ZOUTPUT(:,1)=UNPACK(ZBUF(:),LMASK(ISTP:IENP),RMISS)
  CALL NCVPT (NPOS,NVARID,ISTART2,ICOUNT2,ZOUTPUT(:,1),IERR)
ENDIF

!     snow density
CALL MPL_GATHERV(PRECVBUF=ZBUF(:),KROOT=1,PSENDBUF=RSNNU0(:,1),KRECVCOUNTS=NPOIP(:),CDSTRING="WRTRES:snowdens")
IF( MYPROC == 1 ) THEN
  NVARID = NCVID(NPOS,'snowdens',IERR)
  ZOUTPUT(:,1)=UNPACK(ZBUF(:),LMASK(ISTP:IENP),RMISS)
  CALL NCVPT (NPOS,NVARID,ISTART2,ICOUNT2,ZOUTPUT(:,1),IERR)
ENDIF

!     snow albedo
CALL MPL_GATHERV(PRECVBUF=ZBUF(:),KROOT=1,PSENDBUF=ASNNU0(:),KRECVCOUNTS=NPOIP(:),CDSTRING="WRTRES:SAlbedo")
IF( MYPROC == 1 ) THEN
  NVARID = NCVID(NPOS,'SAlbedo',IERR)
  ZOUTPUT(:,1)=UNPACK(ZBUF(:),LMASK(ISTP:IENP),RMISS)
  CALL NCVPT (NPOS,NVARID,ISTART2,ICOUNT2,ZOUTPUT(:,1),IERR)
ENDIF

!     skin temperature
CALL MPL_GATHERV(PRECVBUF=ZBUF(:),KROOT=1,PSENDBUF=TRENU0(:),KRECVCOUNTS=NPOIP(:),CDSTRING="WRTRES:AvgSurfT")
IF( MYPROC == 1 ) THEN
  NVARID = NCVID(NPOS,'AvgSurfT',IERR)
  ZOUTPUT(:,1)=UNPACK(ZBUF(:),LMASK(ISTP:IENP),RMISS)
  CALL NCVPT (NPOS,NVARID,ISTART2,ICOUNT2,ZOUTPUT(:,1),IERR)
  ! Write out log of restart file to check bit-repro
  CALL MINMAX('AvgSurfT',REAL(ZOUTPUT(:,1),KIND=JPRB),ICOUNT2(1),ICOUNT2(2),LMASK(ISTP:IENP),NULOUT)
ENDIF

IF (LEFLAKE) THEN
  !     Lake ice temperature
  CALL MPL_GATHERV(PRECVBUF=ZBUF(:),KROOT=1,PSENDBUF=TLICENU0(:),KRECVCOUNTS=NPOIP(:),CDSTRING="WRTRES:TLICE")
  IF( MYPROC == 1 ) THEN
    NVARID = NCVID(NPOS,'TLICE',IERR)
    ZOUTPUT(:,1)=UNPACK(ZBUF(:),LMASK(ISTP:IENP),RMISS)
    CALL NCVPT (NPOS,NVARID,ISTART2,ICOUNT2,ZOUTPUT(:,1),IERR)
    ! Write out log of restart file to check bit-repro
    CALL MINMAX('TLICE',REAL(ZOUTPUT(:,1),KIND=JPRB),ICOUNT2(1),ICOUNT2(2),LMASK(ISTP:IENP),NULOUT)
  ENDIF
  
  !     lake mean water temperature
  CALL MPL_GATHERV(PRECVBUF=ZBUF(:),KROOT=1,PSENDBUF=TLMNWNU0(:),KRECVCOUNTS=NPOIP(:),CDSTRING="WRTRES:TLMNW")
  IF( MYPROC == 1 ) THEN
    NVARID = NCVID(NPOS,'TLMNW',IERR)
    ZOUTPUT(:,1)=UNPACK(ZBUF(:),LMASK(ISTP:IENP),RMISS)
    CALL NCVPT (NPOS,NVARID,ISTART2,ICOUNT2,ZOUTPUT(:,1),IERR)
    ! Write out log of restart file to check bit-repro
    CALL MINMAX('TLMNW',REAL(ZOUTPUT(:,1),KIND=JPRB),ICOUNT2(1),ICOUNT2(2),LMASK(ISTP:IENP),NULOUT)
  ENDIF

  !     lake mixed layer temperature
  CALL MPL_GATHERV(PRECVBUF=ZBUF(:),KROOT=1,PSENDBUF=TLWMLNU0(:),KRECVCOUNTS=NPOIP(:),CDSTRING="WRTRES:TLWML")
  IF( MYPROC == 1 ) THEN
    NVARID = NCVID(NPOS,'TLWML',IERR)
    ZOUTPUT(:,1)=UNPACK(ZBUF(:),LMASK(ISTP:IENP),RMISS)
    CALL NCVPT (NPOS,NVARID,ISTART2,ICOUNT2,ZOUTPUT(:,1),IERR)
    ! Write out log of restart file to check bit-repro
    CALL MINMAX('TLWML',REAL(ZOUTPUT(:,1),KIND=JPRB),ICOUNT2(1),ICOUNT2(2),LMASK(ISTP:IENP),NULOUT)
  ENDIF
  
  !     lake bottom temperature
  CALL MPL_GATHERV(PRECVBUF=ZBUF(:),KROOT=1,PSENDBUF=TLBOTNU0(:),KRECVCOUNTS=NPOIP(:),CDSTRING="WRTRES:TLBOT") 
  IF( MYPROC == 1 ) THEN
    NVARID = NCVID(NPOS,'TLBOT',IERR)
    ZOUTPUT(:,1)=UNPACK(ZBUF(:),LMASK(ISTP:IENP),RMISS)
    CALL NCVPT (NPOS,NVARID,ISTART2,ICOUNT2,ZOUTPUT(:,1),IERR)
    ! Write out log of restart file to check bit-repro
    CALL MINMAX('TLBOT',REAL(ZOUTPUT(:,1),KIND=JPRB),ICOUNT2(1),ICOUNT2(2),LMASK(ISTP:IENP),NULOUT)
  ENDIF
  
  !    lake temperature shape factor 
  CALL MPL_GATHERV(PRECVBUF=ZBUF(:),KROOT=1,PSENDBUF=TLSFNU0(:),KRECVCOUNTS=NPOIP(:),CDSTRING="WRTRES:TLSF")
  IF( MYPROC == 1 ) THEN
    NVARID = NCVID(NPOS,'TLSF',IERR)
    ZOUTPUT(:,1)=UNPACK(ZBUF(:),LMASK(ISTP:IENP),RMISS)
    CALL NCVPT (NPOS,NVARID,ISTART2,ICOUNT2,ZOUTPUT(:,1),IERR)
    ! Write out log of restart file to check bit-repro
    CALL MINMAX('TLSF',REAL(ZOUTPUT(:,1),KIND=JPRB),ICOUNT2(1),ICOUNT2(2),LMASK(ISTP:IENP),NULOUT)
  ENDIF

  !     lake ice thickness
  CALL MPL_GATHERV(PRECVBUF=ZBUF(:),KROOT=1,PSENDBUF=HLICENU0(:),KRECVCOUNTS=NPOIP(:),CDSTRING="WRTRES:HLICE")
  IF( MYPROC == 1 ) THEN   
    NVARID = NCVID(NPOS,'HLICE',IERR)
    ZOUTPUT(:,1)=UNPACK(ZBUF(:),LMASK(ISTP:IENP),RMISS)
    CALL NCVPT (NPOS,NVARID,ISTART2,ICOUNT2,ZOUTPUT(:,1),IERR)
    ! Write out log of restart file to check bit-repro
    CALL MINMAX('HLICE',REAL(ZOUTPUT(:,1),KIND=JPRB),ICOUNT2(1),ICOUNT2(2),LMASK(ISTP:IENP),NULOUT)
  ENDIF

  !     lake mixed layer thickness
  CALL MPL_GATHERV(PRECVBUF=ZBUF(:),KROOT=1,PSENDBUF=HLMLNU0(:),KRECVCOUNTS=NPOIP(:),CDSTRING="WRTRES:HLML")
  IF( MYPROC == 1 ) THEN
    NVARID = NCVID(NPOS,'HLML',IERR)
    ZOUTPUT(:,1)=UNPACK(ZBUF(:),LMASK(ISTP:IENP),RMISS)
    CALL NCVPT (NPOS,NVARID,ISTART2,ICOUNT2,ZOUTPUT(:,1),IERR)
    ! Write out log of restart file to check bit-repro
    CALL MINMAX('HLML',REAL(ZOUTPUT(:,1),KIND=JPRB),ICOUNT2(1),ICOUNT2(2),LMASK(ISTP:IENP),NULOUT)
  ENDIF

  !     lake depth
  CALL MPL_GATHERV(PRECVBUF=ZBUF(:),KROOT=1,PSENDBUF=VFLDEPTH(:),KRECVCOUNTS=NPOIP(:),CDSTRING="WRTRES:LDEPTH")
  IF( MYPROC == 1 ) THEN
    NVARID = NCVID(NPOS,'LDEPTH',IERR)
    ZOUTPUT(:,1)=UNPACK(ZBUF(:),LMASK(ISTP:IENP),RMISS)
    CALL NCVPT (NPOS,NVARID,ISTART2,ICOUNT2,ZOUTPUT(:,1),IERR)
  ENDIF

  !     lake cover - lake mask
  CALL MPL_GATHERV(PRECVBUF=ZBUF(:),KROOT=1,PSENDBUF=VFCLAKE(:),KRECVCOUNTS=NPOIP(:),CDSTRING="WRTRES:CLAKE")
  IF( MYPROC == 1 ) THEN
    NVARID = NCVID(NPOS,'CLAKE',IERR)
    ZOUTPUT(:,1)=UNPACK(ZBUF(:),LMASK(ISTP:IENP),RMISS)
    CALL NCVPT (NPOS,NVARID,ISTART2,ICOUNT2,ZOUTPUT(:,1),IERR)
  ENDIF


  !     lake cover - flood fraction
  CALL MPL_GATHERV(PRECVBUF=ZBUF(:),KROOT=1,PSENDBUF=VFCLAKEF(:),KRECVCOUNTS=NPOIP(:),CDSTRING="WRTRES:CLAKEF")
  IF ( MYPROC == 1 ) THEN
    NVARID = NCVID(NPOS,'CLAKEF',IERR)
    ZOUTPUT(:,1)=UNPACK(ZBUF(:),LMASK(ISTP:IENP),RMISS)
    CALL NCVPT (NPOS,NVARID,ISTART2,ICOUNT2,ZOUTPUT(:,1),IERR)
  ENDIF
ENDIF

IF(NDIMCDF == 2)THEN
  ISTART3(1)=IX1
  ISTART3(2)=IY1
  ISTART3(3)=1
  ICOUNT3(1)=IX2-IX1+1
  ICOUNT3(2)=IY2-IY1+1
  ICOUNT3(3)=NCSS
ELSE
  ISTART3(1)=IST
  ISTART3(2)=1
  ICOUNT3(1)=NLEN
  ICOUNT3(2)=NCSS
ENDIF

!     soil temperature

DO JK=1,NCSS
  CALL MPL_GATHERV(PRECVBUF=ZBUF(:),KROOT=1,PSENDBUF=TSLNU0(:,JK),KRECVCOUNTS=NPOIP(:),CDSTRING="WRTRES:SoilTemp")
  IF( MYPROC == 1 ) THEN
    ZOUTPUT(:,JK)=UNPACK(ZBUF(:),LMASK(ISTP:IENP),RMISS)
    ! Write out log of restart file to check bit-repro
    CALL MINMAX('SoilTemp',REAL(ZOUTPUT(:,JK),KIND=JPRB),ICOUNT2(1),ICOUNT2(2),LMASK(ISTP:IENP),NULOUT)
  ENDIF
ENDDO

IF( MYPROC == 1 ) THEN
  NVARID = NCVID(NPOS,'SoilTemp',IERR)
  CALL NCVPT (NPOS,NVARID,ISTART3,ICOUNT3,ZOUTPUT,IERR)
ENDIF

!     soil moisture
DO JK=1,NCSS
  CALL MPL_GATHERV(PRECVBUF=ZBUF(:),KROOT=1,PSENDBUF=ZQLINU0(:,JK),KRECVCOUNTS=NPOIP(:),CDSTRING="WRTRES:SoilMoist")
  IF( MYPROC == 1 ) THEN
    ZOUTPUT(:,JK)=UNPACK(ZBUF(:),LMASK(ISTP:IENP),RMISS)
    ! Write out log of restart file to check bit-repro
    CALL MINMAX('SoilMoist',REAL(ZOUTPUT(:,JK),KIND=JPRB),ICOUNT2(1),ICOUNT2(2),LMASK(ISTP:IENP),NULOUT)
  ENDIF
ENDDO

DEALLOCATE(ZQLINU0)

IF( MYPROC == 1 ) THEN
  NVARID = NCVID(NPOS,'SoilMoist',IERR)
  CALL NCVPT (NPOS,NVARID,ISTART3,ICOUNT3,ZOUTPUT,IERR)
ENDIF

!     sea ice temperature
DO JK=1,NCSS
  CALL MPL_GATHERV(PRECVBUF=ZBUF(:),KROOT=1,PSENDBUF=TILNU0(:,JK),KRECVCOUNTS=NPOIP(:),CDSTRING="WRTRES:icetemp")
  IF( MYPROC == 1 ) THEN
    ZOUTPUT(:,JK)=UNPACK(ZBUF(:),LMASK(ISTP:IENP),RMISS)
  ENDIF
ENDDO

IF( MYPROC == 1 ) THEN
  NVARID = NCVID(NPOS,'icetemp',IERR)
  CALL NCVPT (NPOS,NVARID,ISTART3,ICOUNT3,ZOUTPUT,IERR)
ENDIF

IF (LESNML) THEN
  
  IF(NDIMCDF == 2)THEN
    ICOUNT3(3)=NCSNEC
  ELSE
    ICOUNT3(2)=NCSNEC
  ENDIF
  
  !     SWE
  DO JK=1,NCSNEC
    CALL MPL_GATHERV(PRECVBUF=ZBUF(:),KROOT=1,PSENDBUF=FSNNU0(:,JK),KRECVCOUNTS=NPOIP(:),CDSTRING="WRTRES:SWEML")
    IF( MYPROC == 1 ) THEN
      ZOUTPUT(:,JK)=UNPACK(ZBUF(:),LMASK(ISTP:IENP),RMISS)
      ! Write out log of restart file to check bit-repro
      CALL MINMAX('SWEML',REAL(ZOUTPUT(:,JK),KIND=JPRB),ICOUNT2(1),ICOUNT2(2),LMASK(ISTP:IENP),NULOUT)
    ENDIF
  ENDDO

  IF( MYPROC == 1 ) THEN
    NVARID = NCVID(NPOS,'SWEML',IERR)
    CALL NCVPT (NPOS,NVARID,ISTART3,ICOUNT3,ZOUTPUT,IERR)
  ENDIF

  !     SnowT
  DO JK=1,NCSNEC
    CALL MPL_GATHERV(PRECVBUF=ZBUF(:),KROOT=1,PSENDBUF=TSNNU0(:,JK),KRECVCOUNTS=NPOIP(:),CDSTRING="WRTRES:SnowTML")
    IF( MYPROC == 1 ) THEN
      ZOUTPUT(:,JK)=UNPACK(ZBUF(:),LMASK(ISTP:IENP),RMISS)
      ! Write out log of restart file to check bit-repro
      CALL MINMAX('SnowTML',REAL(ZOUTPUT(:,JK),KIND=JPRB),ICOUNT2(1),ICOUNT2(2),LMASK(ISTP:IENP),NULOUT)
    ENDIF
  ENDDO
 
  IF( MYPROC == 1 ) THEN 
    NVARID = NCVID(NPOS,'SnowTML',IERR)
    CALL NCVPT (NPOS,NVARID,ISTART3,ICOUNT3,ZOUTPUT,IERR)
  ENDIF
  
  !     Snowdensity 
  DO JK=1,NCSNEC
    CALL MPL_GATHERV(PRECVBUF=ZBUF(:),KROOT=1,PSENDBUF=RSNNU0(:,JK),KRECVCOUNTS=NPOIP(:),CDSTRING="WRTRES:snowdensML")
    IF( MYPROC == 1 ) THEN
      ZOUTPUT(:,JK)=UNPACK(ZBUF(:),LMASK(ISTP:IENP),RMISS)
      ! Write out log of restart file to check bit-repro
      CALL MINMAX('SnowDensML',REAL(ZOUTPUT(:,JK),KIND=JPRB),ICOUNT2(1),ICOUNT2(2),LMASK(ISTP:IENP),NULOUT)
    ENDIF
  ENDDO

  IF( MYPROC == 1 ) THEN
    NVARID = NCVID(NPOS,'snowdensML',IERR)
    CALL NCVPT (NPOS,NVARID,ISTART3,ICOUNT3,ZOUTPUT,IERR) 
  ENDIF

  !     Snow liquid water 
  DO JK=1,NCSNEC
    CALL MPL_GATHERV(PRECVBUF=ZBUF(:),KROOT=1,PSENDBUF=WSNNU0(:,JK),KRECVCOUNTS=NPOIP(:),CDSTRING="WRTRES:slwML")
    IF( MYPROC == 1 ) THEN
      ZOUTPUT(:,JK)=UNPACK(ZBUF(:),LMASK(ISTP:IENP),RMISS)
      ! Write out log of restart file to check bit-repro
      CALL MINMAX('SLWML',REAL(ZOUTPUT(:,JK),KIND=JPRB),ICOUNT2(1),ICOUNT2(2),LMASK(ISTP:IENP),NULOUT)
    ENDIF
  ENDDO

  IF( MYPROC == 1 ) THEN
    NVARID = NCVID(NPOS,'slwML',IERR)
    CALL NCVPT (NPOS,NVARID,ISTART3,ICOUNT3,ZOUTPUT,IERR) 
  ENDIF

ENDIF 



!IF (LECTESSEL) THEN

IF(NDIMCDF == 2)THEN
  ICOUNT3(3)=NVHILO
ELSE
  ICOUNT3(2)=NVHILO
ENDIF
!     reference respiration

DO JVT=1,NVHILO
  CALL MPL_GATHERV(PRECVBUF=ZBUF(:),KROOT=1,PSENDBUF=VFR0VT(:,JVT),KRECVCOUNTS=NPOIP(:),CDSTRING="WRTRES:r0vt")
  IF( MYPROC == 1 ) THEN
    ZOUTPUT(:,JVT)=UNPACK(ZBUF(:),LMASK(ISTP:IENP),RMISS)
  ENDIF
ENDDO

IF( MYPROC == 1 ) THEN
  NVARID = NCVID(NPOS,'r0vt',IERR)
  CALL NCVPT (NPOS,NVARID,ISTART3,ICOUNT3,ZOUTPUT,IERR)
ENDIF

!     lai per vegetation type
DO JVT=1,NVHILO
  CALL MPL_GATHERV(PRECVBUF=ZBUF(:),KROOT=1,PSENDBUF=LAINU0(:,JVT),KRECVCOUNTS=NPOIP(:),CDSTRING="WRTRES:laivt")
  IF( MYPROC == 1 ) THEN
    ZOUTPUT(:,JVT)=UNPACK(ZBUF(:),LMASK(ISTP:IENP),RMISS)
    ! Write out log of restart file to check bit-repro
    CALL MINMAX('laivt',REAL(ZOUTPUT(:,JVT),KIND=JPRB),ICOUNT2(1),ICOUNT2(2),LMASK(ISTP:IENP),NULOUT)
  ENDIF
ENDDO

IF( MYPROC == 1 ) THEN
  NVARID = NCVID(NPOS,'laivt',IERR)
  CALL NCVPT (NPOS,NVARID,ISTART3,ICOUNT3,ZOUTPUT,IERR)
ENDIF

!     Bstr per vegetation type
DO JVT=1,NVHILO
  CALL MPL_GATHERV(PRECVBUF=ZBUF(:),KROOT=1,PSENDBUF=BSTRNU0(:,JVT),KRECVCOUNTS=NPOIP(:),CDSTRING="WRTRES:Biomstr")
  IF( MYPROC == 1 ) THEN
    ZOUTPUT(:,JVT)=UNPACK(ZBUF(:),LMASK(ISTP:IENP),RMISS)
    ! Write out log of restart file to check bit-repro
    CALL MINMAX('Biomstr',REAL(ZOUTPUT(:,JVT),KIND=JPRB),ICOUNT2(1),ICOUNT2(2),LMASK(ISTP:IENP),NULOUT)
  ENDIF
ENDDO

IF( MYPROC == 1 ) THEN
  NVARID = NCVID(NPOS,'Biomstr',IERR)
  CALL NCVPT (NPOS,NVARID,ISTART3,ICOUNT3,ZOUTPUT,IERR)
ENDIF

!     Bstr2 per vegetation type
DO JVT=1,NVHILO
  CALL MPL_GATHERV(PRECVBUF=ZBUF(:),KROOT=1,PSENDBUF=BSTR2NU0(:,JVT),KRECVCOUNTS=NPOIP(:),CDSTRING="WRTRES:Biomstr2")
  IF( MYPROC == 1 ) THEN
    ZOUTPUT(:,JVT)=UNPACK(ZBUF(:),LMASK(ISTP:IENP),RMISS)
    ! Write out log of restart file to check bit-repro
    CALL MINMAX('Biomstr2',REAL(ZOUTPUT(:,JVT),KIND=JPRB),ICOUNT2(1),ICOUNT2(2),LMASK(ISTP:IENP),NULOUT)
  ENDIF
ENDDO

IF( MYPROC == 1 ) THEN
  NVARID = NCVID(NPOS,'Biomstr2',IERR)
  CALL NCVPT (NPOS,NVARID,ISTART3,ICOUNT3,ZOUTPUT,IERR)
ENDIF

!* -- Vegetation type specific quantities
!***********************************************************************
IF(NDIMCDF == 2)THEN
  ISTART3(1)=IX1
  ISTART3(2)=IY1
  ISTART3(3)=1
  ICOUNT3(1)=IX2-IX1+1
  ICOUNT3(2)=IY2-IY1+1
  ICOUNT3(3)=NVHILO
ELSE
  ISTART3(1)=IST
  ISTART3(2)=1
  ICOUNT3(1)=NLEN
  ICOUNT3(2)=NVHILO
ENDIF

!     daily net CO2 assimilation
DO JVT=1,NVHILO
  CALL MPL_GATHERV(PRECVBUF=ZBUF(:),KROOT=1,PSENDBUF=VDANDAYVT(:,JVT),KRECVCOUNTS=NPOIP(:),CDSTRING="WRTRES:Anday")
  IF( MYPROC == 1 ) THEN
    ZOUTPUT(:,JVT)=UNPACK(ZBUF(:),LMASK(ISTP:IENP),RMISS)
    ! Write out log of restart file to check bit-repro
    CALL MINMAX('Anday',REAL(ZOUTPUT(:,JVT),KIND=JPRB),ICOUNT2(1),ICOUNT2(2),LMASK(ISTP:IENP),NULOUT)
  ENDIF
ENDDO

IF( MYPROC == 1 ) THEN
  NVARID = NCVID(NPOS,'Anday',IERR)
  CALL NCVPT (NPOS,NVARID,ISTART3,ICOUNT3,ZOUTPUT,IERR)
ENDIF

!     daily maximum leaf assimilation
DO JVT=1,NVHILO
  CALL MPL_GATHERV(PRECVBUF=ZBUF(:),KROOT=1,PSENDBUF=VDANFMVT(:,JVT),KRECVCOUNTS=NPOIP(:),CDSTRING="WRTRES:Anfm")
  IF( MYPROC == 1 ) THEN
    ZOUTPUT(:,JVT)=UNPACK(ZBUF(:),LMASK(ISTP:IENP),RMISS)
    ! Write out log of restart file to check bit-repro
    CALL MINMAX('Anfm',REAL(ZOUTPUT(:,JVT),KIND=JPRB),ICOUNT2(1),ICOUNT2(2),LMASK(ISTP:IENP),NULOUT)
  ENDIF
ENDDO

IF( MYPROC == 1 ) THEN
  NVARID = NCVID(NPOS,'Anfm',IERR)
  CALL NCVPT (NPOS,NVARID,ISTART3,ICOUNT3,ZOUTPUT,IERR)
ENDIF

!     respiration of above ground structural biomass
DO JVT=1,NVHILO
  CALL MPL_GATHERV(PRECVBUF=ZBUF(:),KROOT=1,PSENDBUF=VDRESPBSTR(:,JVT),KRECVCOUNTS=NPOIP(:),CDSTRING="WRTRES:Rstr")
  IF( MYPROC == 1 ) THEN
    ZOUTPUT(:,JVT)=UNPACK(ZBUF(:),LMASK(ISTP:IENP),RMISS)
    ! Write out log of restart file to check bit-repro
    CALL MINMAX('Rstr',REAL(ZOUTPUT(:,JVT),KIND=JPRB),ICOUNT2(1),ICOUNT2(2),LMASK(ISTP:IENP),NULOUT)
  ENDIF
ENDDO

IF( MYPROC == 1 ) THEN
  NVARID = NCVID(NPOS,'Rstr',IERR)
  CALL NCVPT (NPOS,NVARID,ISTART3,ICOUNT3,ZOUTPUT,IERR)
ENDIF

!     respiration of below ground structural biomass
DO JVT=1,NVHILO
  CALL MPL_GATHERV(PRECVBUF=ZBUF(:),KROOT=1,PSENDBUF=VDRESPBSTR2(:,JVT),KRECVCOUNTS=NPOIP(:),CDSTRING="WRTRES:Rstr2")
  IF( MYPROC == 1 ) THEN
    ZOUTPUT(:,JVT)=UNPACK(ZBUF(:),LMASK(ISTP:IENP),RMISS)
    ! Write out log of restart file to check bit-repro
    CALL MINMAX('Rstr2',REAL(ZOUTPUT(:,JVT),KIND=JPRB),ICOUNT2(1),ICOUNT2(2),LMASK(ISTP:IENP),NULOUT)
  ENDIF
ENDDO

IF( MYPROC == 1 ) THEN
  NVARID = NCVID(NPOS,'Rstr2',IERR)
  CALL NCVPT (NPOS,NVARID,ISTART3,ICOUNT3,ZOUTPUT,IERR)
ENDIF

!     biomass of previous day
DO JVT=1,NVHILO
  CALL MPL_GATHERV(PRECVBUF=ZBUF(:),KROOT=1,PSENDBUF=VDBIOMASS_LAST(:,JVT),KRECVCOUNTS=NPOIP(:),CDSTRING="WRTRES:Blast")
  IF( MYPROC == 1 ) THEN
    ZOUTPUT(:,JVT)=UNPACK(ZBUF(:),LMASK(ISTP:IENP),RMISS)
    ! Write out log of restart file to check bit-repro
    CALL MINMAX('Blast',REAL(ZOUTPUT(:,JVT),KIND=JPRB),ICOUNT2(1),ICOUNT2(2),LMASK(ISTP:IENP),NULOUT)
  ENDIF
ENDDO

IF( MYPROC == 1 ) THEN
  NVARID = NCVID(NPOS,'Blast',IERR)
  CALL NCVPT (NPOS,NVARID,ISTART3,ICOUNT3,ZOUTPUT,IERR)
ENDIF

!     synchronize to disk
IF( MYPROC == 1 ) THEN
  IF (LNCSNC) CALL NCSNC(NPOS,IERR)
ENDIF
!***********************************************************************

WRITE(NULOUT,*)'RESTART FILE WRITTEN FOR TIMESTEP: ',NSTEP
DEALLOCATE (ZOUTPUT)        !INFLATED OUTPUT ARRAY

IF (LHOOK) CALL DR_HOOK('WRTRES',1,ZHOOK_HANDLE)

END SUBROUTINE WRTRES
